package com.iflysse.servlet.homepage;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.iflysse.pojo.ColumnInfo;
import com.iflysse.pojo.ContentInfo;
import com.iflysse.pojo.NodeDetail;
import com.iflysse.pojo.SiteInfo;
import com.iflysse.service.ColumnInfoService;
import com.iflysse.service.ContentInfoService;
import com.iflysse.service.SiteInfoService;

/**
 * Servlet implementation class homepageServlet
 */
@WebServlet("/homepageServlet")
public class homepageServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public homepageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("charset=utf-8;");
		response.setCharacterEncoding("utf-8");
		
		String nav = request.getParameter("nav");
 		if(nav != null){
			findcolumn(request, response, nav);
		}
		
		setColumnSession(request, response);
		setContentSession(request,response);
		setSiteSession(request, response);
		request.getRequestDispatcher("/WEB-INF/views/home/newHomePage.jsp").forward(request, response);
	}
	/**
	*
	* @author: Administrator
	* @createTime: 2014年11月28日 上午9:27:54
	* @history:
	* @param request
	* @param response void
	 * @throws IOException 
	 * @throws ServletException 
	*/
	private void findcolumn(HttpServletRequest request,
			HttpServletResponse response,String navName) throws ServletException, IOException {
		
		ColumnInfoService service = new ColumnInfoService();
		List<ColumnInfo> navlist = service.findColumnByName(navName);   //navlist应该只有一个值
		Iterator iter = navlist.iterator();
		int id = 1;
		while(iter.hasNext()){
			ColumnInfo column = (ColumnInfo)iter.next();
			if(column.getPpid() == 1){
				id = column.getId();
			}
		}
		//获得所有的一级目录,根据传过来的参数和一级目录的名字匹配
		List<ColumnInfo> list = service.findAllColumn();
		Iterator it = list.iterator();  
		ColumnInfo column = new ColumnInfo();
		List<ContentInfo> contentlist =new ArrayList<ContentInfo>();
		while(it.hasNext()){
			column = (ColumnInfo)it.next();
			if(column.getPpid() == id){
				ContentInfoService ser = new ContentInfoService();
				List<ContentInfo> content = ser.findByContentByName(column.getName());
				for(ContentInfo cinfo:content){
					String str = NoHTML(cinfo.getContent());  //去除html标签
					
					if(str.length() > 200){
						String cont = str.substring(0, 200) + " ...";
						cinfo.setContent(cont);
					}
				}
				contentlist.addAll(content);
			}   
		} 
//		System.out.println(contentlist);
		request.setAttribute("nodelist", contentlist);
		request.getRequestDispatcher("/WEB-INF/views/home/newZiXun.jsp").forward(request, response);
	}

	
	public void setColumnSession(HttpServletRequest request, HttpServletResponse response){
		   ColumnInfoService service=new ColumnInfoService();
		   List<ColumnInfo> infos=service.findChildrensColumnById(1);
		   List<ColumnInfo> tempList=null;
		   List<NodeDetail<ColumnInfo>> nodeList=new ArrayList<NodeDetail<ColumnInfo>>();
		   for(int i=0;i<infos.size();i++)
		   {
			  NodeDetail<ColumnInfo> node=new NodeDetail<ColumnInfo>();
			  node.setT(infos.get(i));
			  tempList=service.findChildrensColumnById(infos.get(i).getId());
			  node.setList(tempList);
			  nodeList.add(node);
		   }
		   //System.out.println(nodeList);
		   HttpSession session=request.getSession();
		   session.setAttribute("columnMenu",nodeList);
		   
	}
	public void setContentSession(HttpServletRequest request, HttpServletResponse response){
		ContentInfoService service=new ContentInfoService();
		List< ContentInfo> infos=service.findByContentAll();
		//System.out.print(infos);
		 HttpSession session=request.getSession();
		   session.setAttribute("content",infos);
	}
	public void setSiteSession(HttpServletRequest request, HttpServletResponse response){
		SiteInfoService service=new SiteInfoService();
		SiteInfo site=service.findSite();
		HttpSession session=request.getSession();
		   session.setAttribute("site",site);
//		   System.out.print(site);
	}

	/**
	*	去掉html标签
	* @author: Administrator
	* @createTime: 2014年12月2日 下午4:49:20
	* @history:
	* @param Htmlstring
	* @return String
	 */
	public String NoHTML(String Htmlstring){ 
	   Pattern pattern = Pattern.compile("<.+?>");
	   Matcher matcher = pattern.matcher(Htmlstring);
	   String string = matcher.replaceAll("");
	   return string;
	 }

}
